﻿using Chen.IdentityServer4.DbAll;
using IdentityModel;
using IdentityServer4.Models;
using IdentityServer4.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Chen.IdentityServer4.Models;

namespace Chen.IdentityServer4.Extent
{
    public class ProfileService : IProfileService
    {
        private RightDbContext _dbContext = null;
        public ProfileService(RightDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// /
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var claims = context.Subject.Claims.ToList();
            var id = context.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject).Value;
            int.TryParse(id, out int uid);
            var user = await _dbContext.SysUsers.AsQueryable().FirstOrDefaultAsync(x => x.Id == uid);
            if (user != null || user.Id > 0)
            {
                var mulist = new List<SysModule>();
                if (user.IsAdmin == 1)
                {
                    var query = from tmu in _dbContext.SysModules.AsQueryable()
                                select new SysModule
                                {
                                    ModuleName = tmu.ModuleName,
                                    ModuleID = tmu.ModuleID,
                                    PathUrl = tmu.PathUrl
                                };
                    mulist = await query.ToListAsync();
                }
                else
                {
                    var query = from ur in _dbContext.SysuserRoles.AsQueryable()
                                join mu in _dbContext.ModuleAuthorities on ur.RoleID equals mu.RoleID
                                join tmu in _dbContext.SysModules on mu.ModuleID equals tmu.ModuleID
                                where ur.SysUserID == uid && ur.Status == 1 && mu.Status == true
                                select new SysModule
                                {
                                    ModuleName = tmu.ModuleName,
                                    ModuleID = mu.ModuleID,
                                    PathUrl = tmu.PathUrl
                                };
                    mulist = await query.ToListAsync();
                }

                var cllist = new List<Claim>();
                cllist.Add(new Claim(UserClaimTypeConst.UserId, user.Id.ToString()));
                //用户管理员标识
                cllist.Add(new Claim(UserClaimTypeConst.IsSuperAdmin, user.IsAdmin.ToString()));
                cllist.Add(new Claim(UserClaimTypeConst.UserName, user.UserName));
                //权限最后更新时间
                cllist.Add(new Claim(UserClaimTypeConst.UserRightLastUpdateTime, user.UserRightLastUpdateTime.ToString("yyyyMMddHHmmss")));
                //角色权限
                cllist.Add(new Claim(UserClaimTypeConst.UserMenuList, string.Join(",", mulist.Select(x => x.ModuleName))));


                context.IssuedClaims = cllist;
            }

        }

        public Task IsActiveAsync(IsActiveContext context)
        {
            context.IsActive = true;
            return Task.CompletedTask;
        }
    }
}
